var SHOP_API = require('API').shop;
var SHOP_EXTRA_API = require('API').after_sale.shop_extra;
module.exports = ['$scope', '$rootScope', '$location', '$routeParams', '$timeout',
    function ($scope, $rootScope, $location, $routeParams, $timeout) {

        $scope.shops = {};
        $scope.shop = {};
        $scope.mapper = {};

        //获取可用门店
        SHOP_API.all_normal(function (json) {
            $scope.$apply(function () {
                $scope.shops = json;
            })
        });

        $scope.$watch('shop', function (_new, _old) {
            if (_old != _new) {
                //加载新门店数据
                load_shop_mapper(_new.id);
            }
        });

        var load_shop_mapper = function (shop_id) {
            SHOP_EXTRA_API.get_by_shop(shop_id, function (json) {
                $scope.$apply(function () {
                    $scope.mapper = json;
                })
            })
        };

        $scope.submit = function () {
            var shop_id = $scope.shop.id;
            var param = $scope.mapper;
            param.shop_id = shop_id;
            if (param.id) {
                SHOP_EXTRA_API.upd(param.id, param, function () {
                    alert('修改成功');
                })
            } else {
                SHOP_EXTRA_API.add(param, function () {
                    alert('添加成功');
                })
            }
        }
    }];